package com.ydgg.ltar.wechat;

import java.net.URLEncoder;

import com.ydgg.ltar.util.HttpTookit;

public class WeiXinUtil {

	/**
	 * 第一步：用户同意授权，获取code(引导关注者打开如下页面：) 获取 code、state
	 */
	public static String getStartURLToGetCode() {

		String takenUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
		takenUrl = takenUrl.replace("APPID", Param.APPID);
		takenUrl = takenUrl.replace("REDIRECT_URI",
				URLEncoder.encode(Param.REDIRECT_URI));
		// FIXME ： snsapi_userinfo
		takenUrl = takenUrl.replace("SCOPE", "snsapi_userinfo");
		System.out.println(takenUrl);
		return takenUrl;
	}

	/**
	 * 获取access_token、openid 第二步：通过code获取access_token
	 * 
	 * @param code
	 *            url = "https://api.weixin.qq.com/sns/oauth2/access_token
	 *            ?appid=APPID &secret=SECRET &code=CODE
	 *            &grant_type=authorization_code"
	 * */
	public static OAuthInfo getAccess_token(String code) {

		String authUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code ";
		authUrl = authUrl.replace("APPID", Param.APPID);
		authUrl = authUrl.replace("SECRET", Param.SECRET);
		authUrl = authUrl.replace("CODE", code);
//		String jsonString = HTTPRequestUtil.sendPost(authUrl, "");
		String jsonString = HttpTookit.doPost(authUrl, null);
		System.out.println("jsonString: " + jsonString);
		OAuthInfo auth = null;
		try {
//			auth = (OAuthInfo) JacksonUtil.parseJSONToObject(OAuthInfo.class,
//					jsonString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return auth;
	}


}
